// pages/index/index.js
Page({
    data: {
      username: '',
      password: '',
      showPassword: false,
      isLoginDisabled: true
    },
  
    onUsernameInput(e) {
      this.setData({
        username: e.detail.value
      });
      this.checkLoginButton();
    },
  
    onPasswordInput(e) {
      this.setData({
        password: e.detail.value
      });
      this.checkLoginButton();
    },
  
    checkLoginButton() {
      const { username, password } = this.data;
      this.setData({
        isLoginDisabled: !(username && password)
      });
    },
  
    onLogin() {
      const { username, password } = this.data;
      
      if (!username || !password) {
        wx.showToast({
          title: '请输入账号密码',
          icon: 'none'
        });
        return;
      }
  
      wx.showLoading({
        title: '登录中...',
      });
  
      // 模拟数据库验证
      const users = wx.getStorageSync('users') || [];
      const user = users.find(u => u.username === username && u.password === password);
      
      setTimeout(() => {
        wx.hideLoading();
        if (user) {
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            success: () => {
              // 保存登录状态
              wx.setStorageSync('currentUser', user);
              // 跳转到首页
              wx.reLaunch({
                url: '/pages/logs/logs'
              });
            }
          });
        } else {
          wx.showToast({
            title: '账号或密码错误',
            icon: 'none'
          });
        }
      }, 1500);
    },
  
    onGoRegister() {
      wx.navigateTo({
        url: '/pages/register/register'
      });
    },
  
    onAgreementTap() {
      wx.navigateTo({
        url: '/pages/agreement/agreement'
      });
    },
  
    onPrivacyTap() {
      wx.navigateTo({
        url: '/pages/privacy/privacy'
      });
    }
  })